import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
  {
    path: '/',
    redirect: '/login'
  }, {
    path: '/login',
    name: 'login',
    component: () => import('../views/login'),
    meta: {
      keepAlive: false
    }
  }, {
    path: '/register',
    name: 'register',
    component: () => import('../views/register'),
    meta: {
      keepAlive: false
    }
  }, {
    path: '/course/:courseId',
    name: 'CourseDetail',
    component: () => import('../views/courseDetail'),
    meta: {
      keepAlive: true
    },
    props: true
  }, {
    path: '/courseS/:courseId',
    name: 'CourseDetailStudent',
    component: () => import('../views/courseDetailStudent'),
    meta: {
      keepAlive: true
    },
    props: true
  }, {
    path: '/homework/:homeworkId',
    name: 'HomeworkDetail',
    component: () => import('../views/homeworkDetail'),
    meta: {
      keepAlive: true
    },
    props: true
  }, {
    path: '/homeworkCoding/:homeworkId',
    name: 'HomeworkDetailCodingTeacher',
    component: () => import('../views/homeworkDetailCodingTeacher'),
    meta: {
      keepAlive: true
    },
    props: true
  }, {
    path: '/homeworkS/:homeworkId',
    name: 'HomeworkDetailS',
    component: () => import('../views/homeworkDetailStudent'),
    beforeRouteLeave (to, from, next) {
      if (from && from.matched.length > 0) {
        const componentInstance = from.matched[0].instances.default
        if (componentInstance) {
          componentInstance.$destroy()
        }
      }
      next()
    },
    meta: {
      keepAlive: true
    },
    props: true
  }, {
    path: '/homeworkSC/:homeworkId',
    name: 'HomeworkDetailSC',
    component: () => import('../views/homeworkDetailCoding'),
    beforeRouteLeave (to, from, next) {
      if (from && from.matched.length > 0) {
        const componentInstance = from.matched[0].instances.default
        if (componentInstance) {
          componentInstance.$destroy()
        }
      }
      next()
    },
    meta: {
      keepAlive: false
    },
    props: true
  }, {
    path: '/correct/:homeworkId',
    name: 'correct',
    component: () => import('../views/correct'),
    meta: {
      keepAlive: true
    }
  }, {
    path: '/infoChange',
    name: 'infoChange',
    component: () => import('../views/infoChange'),
    meta: {
      keepAlive: true
    }
  }, {
    path: '/pswChange',
    name: 'pswChange',
    component: () => import('../views/pswChange'),
    meta: {
      keepAlive: true
    }
  }, {
    path: '/studentCourseManage',
    name: 'studentCourseManage',
    component: () => import('../views/studentCourseManage'),
    meta: {
      keepAlive: true
    }
  }, {
    path: '/teacherCourseManage',
    name: 'teacherCourseManage',
    component: () => import('../views/teacherCourseManage'),
    meta: {
      keepAlive: true
    }
  }, {
    path: '/studentHomeworkManage',
    name: 'studentHomeworkManage',
    component: () => import('../views/studentHomeworkManage'),
    meta: {
      keepAlive: true
    }
  }, {
    path: '/teacherHomeworkManage',
    name: 'teacherHomeworkManage',
    component: () => import('../views/teacherHomeworkManage'),
    meta: {
      keepAlive: true
    }
  }, {
    path: '/onlineJudge/:homeworkId-:gradeId',
    name: 'onlineJudge',
    component: () => import('../views/onlineJudge'),
    meta: {
      keepAlive: true
    },
    props: true
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router
